<template>
  <div class="security-container">
    <Resize v-if="['ty', 'dev'].includes(city_name)">
      <template v-slot:left>
        <div class="content_l">
          <vxe-table
            highlight-current-row
            highlight-hover-row
            :show-header="false"
            show-overflow
            ref="xTree"
            border="inner"
            :data="siteList"
            :tree-config="{
              children: 'children',
              transform: false,
              parentKey: 'parentId',
              line: true,
              iconOpen: 'el-icon-remove',
              iconClose: 'el-icon-circle-plus',
            }"
            class="left-table"
          >
            <vxe-column tree-node show-overflow>
              <template #default="{ row }">
                <span>
                  <span v-if="row.siteId == currentRowId" class="activeRow">
                    <svg-icon icon-class="right" style="color: #009a94" />
                  </span>
                  <!-- 电脑 -->
                  <img
                    src="../../../assets/icons/png/Display Check.png"
                    alt=""
                    style="vertical-align: text-bottom"
                    v-if="row.root == '000'"
                  />
                  <!-- 地球 -->
                  <img
                    src="../../../assets/icons/png/globe.gif"
                    alt=""
                    style="vertical-align: bottom"
                    v-if="!row.channelId && row.root != '000'"
                  />

                  <!-- <i class="tree-node-icon fa" :class="$refs.xTree.isTreeExpandByRow(row) ? 'el-icon-folder-opened' : 'el-icon-folder'"></i> -->
                  <span
                    @click="clickListRow(row)"
                    style="cursor: pointer; fontsize: 12px; color: #000"
                  >
                    {{ row.name }}</span
                  >
                </span>
              </template>
            </vxe-column>
          </vxe-table>
        </div>
      </template>
      <template v-slot:right>
        <div class="content_r">
          <el-row :gutter="10" class="mb8 grid-content" style="">
            <el-col :span="1.5" class="">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['system:dept:add']"
                >新增</el-button
              >
            </el-col>
            <right-toolbar
              @queryTable="clickListRow(siteRow)"
              :showSearchBtn="false"
            ></right-toolbar>
          </el-row>

          <el-table :data="securityList" v-loading="loading">
            <span slot="empty" v-if="!siteRow">请先选择左侧站点</span>
            <el-table-column
              prop="name"
              label="事故名"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="accidentDate"
              label="事故发生时间"
              width="200"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="descripation"
              label="事故描述"
              show-overflow-tooltip
              align="center"
            ></el-table-column>
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
              width="150"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:accident:save']"
                  >修改</el-button
                >
                <el-button
                  v-if="scope.row.parentId != 0"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:accident:delete']"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="clickListRow"
          />
        </div>
      </template>
    </Resize>

    <span class="no_pro_menu" v-else>暂无该功能，仅对太原开放</span>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog
      v-dialogDrag
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="事故名称:" prop="name">
          <el-input v-model="form.name" placeholder="事故名称" />
        </el-form-item>
        <el-form-item label="发生时间" prop="accidentDate">
          <el-date-picker
            v-model="form.accidentDate"
            type="datetime"
            placeholder="发生时间"
            value-format="yyyy-MM-dd HH-mm-ss"
            :style="{ width: '100%', border: 'none' }"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="事件描述" prop="descripation">
          <el-input v-model="form.descripation" placeholder="事件描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="cyan" @click="reset" v-if="false">重 置</el-button>
        <el-button type="cyan" @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSite } from "@/api/content/net";
import {
  securityList as getSecurityList,
  addAccident,
  deleteAccident,
} from "@/api/extendApplication/security";
export default {
  inject: ["city_name"],
  data() {
    return {
      loading: false,
      //当前站点
      siteRow: null,

      siteList: [],
      //右侧表格天数
      securityList: [],
      // 查询参数
      queryParams: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      total: 0,
      form: {
        descripation: "",
        accidentDate: new Date(),
        name: "",
      },
      rules: {
        accidentDate: [
          { required: true, message: "	事故发生不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "事故名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {},

  computed: {
    currentRowId() {
      return this.siteRow?.siteId;
    },
  },

  mounted() {
    ["ty"].includes(this.ity_name) && this.getSiteList();
  },

  methods: {
    //获取站点列表
    getSiteList() {
      listSite()
        .then((res) => {
          this.siteList = [
            { name: "WYCMS主站", root: "000", children: res.data },
          ];
        })
        .then(() => {
          //展开第一个节点
          this.$nextTick(() => {
            this.$refs.xTree.setTreeExpand(this.siteList[0], true);
          });
        });
    },

    //点击站点
    clickListRow(row) {
      if (!row) {
        this.$confirm(`请先选择左侧站点`, "警告", {
          confirmButtonText: "确定",

          type: "warning",
        })
          .then(() => {})
          .catch(() => {
            return;
          });
        return;
      }
      if (row.root == "000") return;

      this.siteRow = row;
      this.loading = true;
      this.queryParams.siteId = this.siteRow.siteId;
      this.queryParams.pageNum = 1;
      this.form.siteId = row.siteId;
      getSecurityList(this.queryParams).then((res) => {
        this.securityList = res.data.list;
        this.total = res.data.totalCount;
        this.loading = false;
      });
    },
    //新增事故
    handleAdd() {
      if (!this.siteRow) {
        this.msgInfo("请先选择左侧站点");
        return;
      }
      this.reset();
      this.open = true;
      this.title = "新增事故";
    },
    handleUpdate(row) {
      this.form = row;
      this.title = "修改事故";
      this.open = true;
    },
    handleDelete(row) {
      this.$confirm(
        '是否确认删除事故名称 " ' + row.name + ' " 的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteAccident({ id: row.id }).then((res) => {
            this.clickListRow(this.siteRow);
            this.msgSuccess("删除成功");
          });
        })

        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addAccident(this.form).then((response) => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.clickListRow(this.siteRow);
          });
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.title = "";
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        accidentDate: undefined,
        descripation: undefined,
        name: undefined,
        siteId: this.currentRowId,
      };
      this.resetForm("form");
    },
  },
};
</script>

<style scoped lang="scss">
@import "~@/assets/styles/variables.scss";
.security-container {
  width: 100%;
  // height: calc(100vh - 100px);
  // height: calc(100vh - #{$footHeight} - 0px - 50px - 20px);
  display: flex;
  flex-direction: row;
  // padding: 0px 10px 10px 0;
  border: 1px solid #ccc;
  position: relative;
  .content_l {
    overflow: auto;
    width: 100%;
    height: 100%;
    background: #f4f4f4 !important;
    display: flex;
    flex-direction: column;
    .left-table {
      flex: 1;
      height: 0;
      overflow: overlay;
      overflow: auto;
    }
  }
  .content_r {
    overflow: overlay;
    overflow: auto;
    // padding: 10px;
    border-left: 1px solid #ccc;
    height: 100%;
    width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
  }
}
</style>
